package com.xx.doublexchat.common.user.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.doublexchat.common.common.domain.vo.request.CursorPageBaseRequest;
import com.xx.doublexchat.common.common.domain.vo.response.CursorPageBaseResponse;
import com.xx.doublexchat.common.common.utils.CursorUtils;
import com.xx.doublexchat.common.user.domain.entity.UserFriend;
import com.xx.doublexchat.common.user.domain.vo.response.FriendResponse;
import com.xx.doublexchat.common.user.mapper.UserFriendMapper;
import com.xx.doublexchat.common.user.service.IUserFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 用户联系人表 服务实现类
 * </p>
 *
 * @author xiaoXuan
 * @since 2024-02-21
 */
@Service
public class UserFriendDao extends ServiceImpl<UserFriendMapper, UserFriend> {

    /**
     * 游标获取好友列表
     *
     * @param uid
     * @param request
     * @return
     */
    public CursorPageBaseResponse<UserFriend> getFriendPage(Long uid, CursorPageBaseRequest request) {
        /*
        等价于下面的通用方法
        //获取游标
        String cursor = request.getCursor();
        LambdaQueryChainWrapper<UserFriend> wrappers = lambdaQuery();
        // 查找我的好友
        wrappers.eq(UserFriend::getUid, uid);
        //获取游标数据
        if (StringUtils.isNotBlank(request.getCursor())) {
            wrappers.lt(UserFriend::getId, cursor);
        }
        // id降序
        wrappers.orderByDesc(UserFriend::getId);
        //分页
        Page<UserFriend> page = page(request.plusPage(), wrappers);
        //获取新游标
        String newCursor = Optional.ofNullable(CollectionUtil.getLast(page.getRecords()))
                .map(UserFriend::getId)
                .map(String::valueOf).orElse(null);
        Boolean isLast = page.getRecords().size() != request.getPageSize();
        CursorPageBaseResponse<UserFriend> cursorPageBaseResponse = new CursorPageBaseResponse<>(newCursor, isLast, page.getRecords());
        */
        return CursorUtils.getCursorPageByMysql(this, request,
                wrapper -> wrapper.eq(UserFriend::getUid, uid), UserFriend::getId);
    }

    /**
     * 通过id获取好友信息
     *
     * @param uid
     * @param uidList
     * @return
     */
    public List<UserFriend> getByFriends(Long uid, List<Long> uidList) {
        return lambdaQuery()
                .eq(UserFriend::getUid, uid)
                .in(UserFriend::getFriendUid, uidList)
                .list();
    }

    /**
     * 判断是否是好友
     *
     * @param uid
     * @param targetUid
     * @return
     */
    public boolean validateFriend(Long uid, Long targetUid) {
        return lambdaQuery()
                .eq(UserFriend::getUid, uid)
                .eq(UserFriend::getFriendUid, targetUid)
                .count() > 0;
    }

    /**
     * 获取好友信息
     *
     * @param uid
     * @param friendUid
     * @return
     */
    public List<UserFriend> getUserFriend(Long uid, Long friendUid) {
        return lambdaQuery()
                .eq(UserFriend::getUid, uid)
                .eq(UserFriend::getFriendUid, friendUid)
                .or()
                .eq(UserFriend::getFriendUid, uid)
                .eq(UserFriend::getUid, friendUid)
                .select(UserFriend::getId)
                .list();
    }
}
